package com.hxkj.component;

import com.cloopen.rest.sdk.CCPRestSDK;
import com.hxkj.dao.CommonMessageDao;
import com.hxkj.domain.enums.MessageStatusEnum;
import com.hxkj.domain.po.CommonMessage;
import com.hxkj.utils.UUIDUtil;
import com.platform.common.domain.result.ResultDataStruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Set;

/**
 * cloopen短信组件
 * <p>
 * Create by chenwei on 2018/7/27 10:30
 */
@Component
@Slf4j
public class CloopenMessageComponent {
    private final static String SERVER_IP = "app.cloopen.com";
    private final static String SERVER_PORT = "8883";
    private final static String ACCOUNT_ID = "8aaf070855dd176e0155ddd8a2560182";
    private final static String ACCOUNT_TOKEN = "dfa8cb9ab797449fb05981cbc34b1e0f";
    private final static String APP_ID = "8aaf070855dd176e0155ddd8a2bb0188";
    private final static String TEMPLATE_ID = "283862";

    @Autowired
    private CommonMessageDao commonMessageDao;

    public ResultDataStruct sendMessage(CommonMessage commonMessage) {
        CCPRestSDK restAPI = new CCPRestSDK();
        restAPI.init(SERVER_IP, SERVER_PORT);
        restAPI.setAccount(ACCOUNT_ID, ACCOUNT_TOKEN);
        restAPI.setAppId(APP_ID);

        // 发送短信
        HashMap<String, Object> result = restAPI.sendTemplateSMS(commonMessage.getTelephone(), TEMPLATE_ID, new String[]{commonMessage.getMessage()});
        log.info("SDKTestSendTemplateSMS result=" + result);

        if ("000000".equals(result.get("statusCode"))) {
            HashMap<String, Object> data = (HashMap<String, Object>) result.get("data");
            Set<String> keySet = data.keySet();
            for (String key : keySet) {
                Object object = data.get(key);
                log.info(key + " = " + object);
            }
            CommonMessage message = CommonMessage.builder()
                    .id(UUIDUtil.generateUUID())
                    .createTime(System.currentTimeMillis())
                    .updataTime(System.currentTimeMillis())
                    .telephone(commonMessage.getTelephone())
                    .message(commonMessage.getMessage())
                    .ip(commonMessage.getIp())
                    .messageType(commonMessage.getMessageType())
                    .status(MessageStatusEnum.SUCCESS.getType())
                    .response("发送成功")
                    .serviceId(commonMessage.getServiceId()).build();
            commonMessageDao.save(message);
            ResultDataStruct resultDataStruct = new ResultDataStruct();
            resultDataStruct.setMessage("发送成功");
            resultDataStruct.setCode(200);
            return resultDataStruct;
        } else {
            log.error("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
            CommonMessage message = CommonMessage.builder()
                    .id(UUIDUtil.generateUUID())
                    .createTime(System.currentTimeMillis())
                    .updataTime(System.currentTimeMillis())
                    .telephone(commonMessage.getTelephone())
                    .message(commonMessage.getMessage())
                    .ip(commonMessage.getIp())
                    .status(MessageStatusEnum.FAILURE.getType())
                    .messageType(commonMessage.getMessageType())
                    .response((String) result.get("statusMsg"))
                    .serviceId(commonMessage.getServiceId()).build();
            commonMessageDao.save(message);
            ResultDataStruct resultDataStruct = new ResultDataStruct();
            resultDataStruct.setMessage((String) result.get("statusMsg"));
            resultDataStruct.setCode(Integer.parseInt(String.valueOf(result.get("statusCode"))));
            return resultDataStruct;
        }
    }
}
